#!/usr/bin/python
#coding:utf-8
import sqlite3

import time
from time import sleep
import json
import requests
from wxpy import *
import json
import os,sys,md5

bot = Bot( cache_path=True)
conn = sqlite3.connect('./db/qianka')
c = conn.cursor()
print "Opened database successfully";

def select ():
  print '--select--'
  sql="SELECT * from qianka0 where isSend=0"
  print sql
  cursor = c.execute(sql)
  results = cursor.fetchall()
  print u'查找到'+str(len(results))+u'条'
  if len(results)>0:
    sendwx(results[0][1],results[0][2],results[0][3])
    id=results[0][0]
    update(id)

def update(id):
  print '--update--id--'+str(id)+'--'
  sql='update qianka0 set isSend=1 where id='+str(id)
  print sql
  conn.execute(sql)
  conn.commit()

def sendwx(sendName,appName,appSrc):
  print '--sendwx--'
  print sendName
  print appName
  print appSrc
  my_friend = bot.friends().search(sendName)[0]
  print my_friend
  my_friend.send(u'App名称:'+appName+u'\n下载地址:'+appSrc)

def main():
  while 1==1:
    t = time.time()
    print (int(round(t * 1000)))
    select()
    sleep(1)

main()
